package ru.usu.gv.uicomponents.viewitems.edge;

import ru.usu.gv.uicomponents.UIElementBase;
import ru.usu.gv.uicomponents.UIVertex;
import ru.usu.gv.uicomponents.viewitems.ViewItemBase;

import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.IndexedGeometryArray;
import javax.media.j3d.IndexedLineArray;
import javax.media.j3d.LineArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

import java.awt.Color;

/**
 * @author spupyrev
 * 03.11.2008
 */
public class LineEdgeView extends ViewItemBase implements IEdgeViewItem
{
	private UIVertex v1;
	private UIVertex v2;
	private Shape3D shape;
	private IndexedGeometryArray line;

	public LineEdgeView(Appearance appearance, TransformGroup transformGroup, UIVertex v1, UIVertex v2)
	{
		super(transformGroup, appearance, appearance.getMaterial());

		this.v1 = v1;
		this.v2 = v2;

		int settings = GeometryArray.COORDINATES | GeometryArray.COLOR_3;
		line = new IndexedLineArray(2, settings, 2);
		line.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
		line.setCapability(LineArray.ALLOW_COLOR_WRITE);
		line.setCoordinateIndex(0, 0);
		line.setCoordinateIndex(1, 1);

		shape = new Shape3D(line);
		transformGroup.addChild(shape);
	}

	public Geometry buildGeometry()
	{
		line.setCoordinates(0, new Point3d[] { v1.getPosition(), v2.getPosition() });
		return null;
	}

	@Override
	public void setColor(Color color)
	{
		super.setColor(color);
		Color3f color3f = new Color3f(color);
		line.setColors(0, new Color3f[] { color3f, color3f });
	}

	public Point3d[] getControlPoints()
	{
		return new Point3d[] {};
	}

	public void moveToCanvasPosition(TransformGroup transformGroup, int x, int y)
	{
		//pass
	}

	public void rectificate()
	{
		//pass
	}

	public void setControlPoints(Point3d[] controlPoints)
	{
		//pass
	}

	public void makePickable(UIElementBase ge)
	{
		ge.makePickable(shape);
	}

	public double getWidth()
	{
		return 0.0;
		//throw new UnsupportedOperationException();
	}

	public void setWidth(double width)
	{
		//throw new UnsupportedOperationException();
	}

}
